package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.BaseCapacityRule;
import com.sailfish.springbootdemo.pojo.db1.LineTechCapacityValue;
import com.sailfish.springbootdemo.pojo.db1.TechCapacityRule;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface TechCapacityRuleDao {

    /**
     * 查询所有的
     *
     * @param searchValue
     * @return
     */
    @Results(id = "techCapacityRuleResultMap", value = {
            @Result(column = "tech_capacity_rule_id", property = "techCapacityRuleId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_tech_param_key", property = "lineTechParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "base_capacity_param_key", property = "baseCapacityParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_capacity_rule_type", property = "techCapacityRuleType", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_capacity_rule_name", property = "techCapacityRuleName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_tech_big_type_id", property = "lineTechBigTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_tech_small_type_id", property = "lineTechSmallTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_capacity_param_fill_type", property = "techCapacityParamFillType", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_capacity_param_options", property = "techCapacityParamOptions", jdbcType = JdbcType.VARCHAR),
            @Result(column = "actual_equal_declare", property = "actualEqualDeclare", jdbcType = JdbcType.INTEGER),
            @Result(column = "params_notes", property = "paramsNotes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "params_placeholder", property = "paramsPlaceholder", jdbcType = JdbcType.VARCHAR),
            @Result(column = "params_picture", property = "paramsPicture", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_required", property = "isRequired", jdbcType = JdbcType.INTEGER),
            @Result(column = "bis_keys", property = "bisKeys", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_related_pcba", property = "isRelatedPcba", jdbcType = JdbcType.INTEGER),
            @Result(column = "can_edit", property = "canEdit", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.TECH_CAPACITY_RULE + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and (tech_capacity_rule_name like CONCAT('%',#{searchValue},'%') or line_tech_param_key like CONCAT('%',#{searchValue},'%'))" +
            "</if>" +
            "<if test='lineTechBigTypeId != null and lineTechBigTypeId != \"\" '>" +
            " and line_tech_big_type_id = #{lineTechBigTypeId}" +
            "</if>" +
            "<if test='lineTechSmallTypeId != null and lineTechSmallTypeId != \"\" '>" +
            " and line_tech_small_type_id = #{lineTechSmallTypeId}" +
            "</if>" +
            " order by sort" +
            "</script>")
    List<TechCapacityRule> getAllTechCapacityRule(@Param("searchValue") String searchValue, @Param("lineTechBigTypeId") Integer lineTechBigTypeId, @Param("lineTechSmallTypeId") Integer lineTechSmallTypeId);

    @ResultMap(value = "techCapacityRuleResultMap")
    @Select("select * from " + DB1TableName.TECH_CAPACITY_RULE + " where tech_capacity_rule_name = #{techCapacityRuleName} and line_tech_big_type_id = #{lineTechBigTypeId} and line_tech_small_type_id = #{lineTechSmallTypeId}")
    List<TechCapacityRule> getTechCapacityRuleByNameAndType(@Param("techCapacityRuleName") String techCapacityRuleName, @Param("lineTechBigTypeId") Integer lineTechBigTypeId, @Param("lineTechSmallTypeId") Integer lineTechSmallTypeId);

    /**
     * 根据key查询规则
     *
     * @param lineTechParamKey
     * @return
     */
    @ResultMap(value = "techCapacityRuleResultMap")
    @Select("select * from " + DB1TableName.TECH_CAPACITY_RULE + " where line_tech_param_key = #{lineTechParamKey}")
    TechCapacityRule getTechCapacityRuleByKey(@Param("lineTechParamKey") String lineTechParamKey);

    /**
     * 根据id查询规则
     *
     * @param techCapacityRuleId
     * @return
     */
    @ResultMap(value = "techCapacityRuleResultMap")
    @Select("select * from " + DB1TableName.TECH_CAPACITY_RULE + " where tech_capacity_rule_id = #{techCapacityRuleId}")
    TechCapacityRule getTechCapacityRuleById(@Param("techCapacityRuleId") Integer techCapacityRuleId);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "techCapacityRuleResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.TECH_CAPACITY_RULE + " where tech_capacity_rule_name = #{techCapacityRuleName} and line_tech_big_type_id = #{lineTechBigTypeId} and line_tech_small_type_id = #{lineTechSmallTypeId} " +
            "<if test='techCapacityRuleId != null and techCapacityRuleId != \"\" '>" +
            " and tech_capacity_rule_id != #{techCapacityRuleId}" +
            "</if>" +
            "</script>")
    List<TechCapacityRule> judgeUniqueTechCapacityRule(@Param("lineTechBigTypeId") Integer lineTechBigTypeId, @Param("lineTechSmallTypeId") Integer lineTechSmallTypeId, @Param("techCapacityRuleId") Integer techCapacityRuleId, @Param("techCapacityRuleName") String techCapacityRuleName);


    /**
     * 新增
     *
     * @param techCapacityRule
     * @return
     */
    @Insert("insert into " + DB1TableName.TECH_CAPACITY_RULE + "(line_tech_param_key,base_capacity_param_key,tech_capacity_rule_type,create_time,sort,status,tech_capacity_rule_name,line_tech_big_type_id,line_tech_small_type_id,tech_capacity_param_fill_type,tech_capacity_param_options,actual_equal_declare,params_notes,params_placeholder,params_picture,is_required,bis_keys,is_related_pcba,can_edit) values " +
            "(#{techCapacityRule.lineTechParamKey},#{techCapacityRule.baseCapacityParamKey},#{techCapacityRule.techCapacityRuleType},#{techCapacityRule.createTime},#{techCapacityRule.sort},#{techCapacityRule.status},#{techCapacityRule.techCapacityRuleName},#{techCapacityRule.lineTechBigTypeId},#{techCapacityRule.lineTechSmallTypeId},#{techCapacityRule.techCapacityParamFillType},#{techCapacityRule.techCapacityParamOptions},#{techCapacityRule.actualEqualDeclare},#{techCapacityRule.paramsNotes},#{techCapacityRule.paramsPlaceholder},#{techCapacityRule.paramsPicture},#{techCapacityRule.isRequired},#{techCapacityRule.bisKeys},#{techCapacityRule.isRelatedPcba},#{techCapacityRule.canEdit})")
    Integer addTechCapacityRule(@Param("techCapacityRule") TechCapacityRule techCapacityRule);

    /**
     * 删除
     *
     * @param techCapacityRuleId
     * @return
     */
    @Delete("delete from " + DB1TableName.TECH_CAPACITY_RULE + " where tech_capacity_rule_id = #{techCapacityRuleId}")
    Integer deleteTechCapacityRuleById(@Param("techCapacityRuleId") Integer techCapacityRuleId);

    /**
     * 更新
     *
     * @param techCapacityRule
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.TECH_CAPACITY_RULE +
            "<set>" +
            "<if test='techCapacityRule.lineTechParamKey != null and techCapacityRule.lineTechParamKey != \"\" '>" +
            " line_tech_param_key = #{techCapacityRule.lineTechParamKey}," +
            "</if>" +
            "<if test='techCapacityRule.baseCapacityParamKey != null and techCapacityRule.baseCapacityParamKey != \"\" '>" +
            " base_capacity_param_key = #{techCapacityRule.baseCapacityParamKey}," +
            "</if>" +
            "<if test='techCapacityRule.techCapacityRuleType != null and techCapacityRule.techCapacityRuleType != \"\" '>" +
            " tech_capacity_rule_type = #{techCapacityRule.techCapacityRuleType}," +
            "</if>" +
            "<if test='techCapacityRule.sort != null and techCapacityRule.sort != \"\" '>" +
            " sort = #{techCapacityRule.sort}," +
            "</if>" +
            "<if test='techCapacityRule.status != null and techCapacityRule.status != \"\" '>" +
            " status = #{techCapacityRule.status}," +
            "</if>" +
            "<if test='techCapacityRule.techCapacityRuleName != null and techCapacityRule.techCapacityRuleName != \"\" '>" +
            " tech_capacity_rule_name = #{techCapacityRule.techCapacityRuleName}," +
            "</if>" +
            "<if test='techCapacityRule.updateTime != null '>" +
            " update_time = #{techCapacityRule.updateTime}," +
            "</if>" +
            "<if test='techCapacityRule.lineTechBigTypeId != null and techCapacityRule.lineTechBigTypeId != \"\" '>" +
            " line_tech_big_type_id = #{techCapacityRule.lineTechBigTypeId}," +
            "</if>" +
            "<if test='techCapacityRule.lineTechSmallTypeId != null and techCapacityRule.lineTechSmallTypeId != \"\" '>" +
            " line_tech_small_type_id = #{techCapacityRule.lineTechSmallTypeId}," +
            "</if>" +
            "<if test='techCapacityRule.techCapacityParamFillType != null and techCapacityRule.techCapacityParamFillType != \"\" '>" +
            " tech_capacity_param_fill_type = #{techCapacityRule.techCapacityParamFillType}," +
            "</if>" +
            "<if test='techCapacityRule.techCapacityParamOptions != null and techCapacityRule.techCapacityParamOptions != \"\" '>" +
            " tech_capacity_param_options = #{techCapacityRule.techCapacityParamOptions}," +
            "</if>" +
            "<if test='techCapacityRule.actualEqualDeclare != null and techCapacityRule.actualEqualDeclare != \"\" '>" +
            " actual_equal_declare = #{techCapacityRule.actualEqualDeclare}," +
            "</if>" +
            "<if test='techCapacityRule.paramsNotes != null '>" +
            " params_notes = #{techCapacityRule.paramsNotes}," +
            "</if>" +
            "<if test='techCapacityRule.paramsPlaceholder != null '>" +
            " params_placeholder = #{techCapacityRule.paramsPlaceholder}," +
            "</if>" +
            "<if test='techCapacityRule.paramsPicture != null '>" +
            " params_picture = #{techCapacityRule.paramsPicture}," +
            "</if>" +
            "<if test='techCapacityRule.isRequired != null and techCapacityRule.isRequired != \"\" '>" +
            " is_required = #{techCapacityRule.isRequired}," +
            "</if>" +
            "<if test='techCapacityRule.canEdit != null and techCapacityRule.canEdit != \"\" '>" +
            " can_edit = #{techCapacityRule.canEdit}," +
            "</if>" +
            "<if test='techCapacityRule.bisKeys != null '>" +
            " bis_keys = #{techCapacityRule.bisKeys}," +
            "</if>" +
            "</set>" +
            " where tech_capacity_rule_id = #{techCapacityRule.techCapacityRuleId}" +
            "</script>")
    Integer updateTechCapacityRule(@Param("techCapacityRule") TechCapacityRule techCapacityRule);

    /**
     * 修改
     */
    @Update("update " + DB1TableName.TECH_CAPACITY_RULE + " set actual_equal_declare  = #{newActualEqualDeclare} where tech_capacity_rule_id = #{techCapacityRuleId}")
    Integer changeActualEqualDeclare(@Param("techCapacityRuleId") Integer techCapacityRuleId, @Param("newActualEqualDeclare") Integer newActualEqualDeclare);

    /**
     * 更新是否关联pcba能力判断
     *
     * @return
     */
    @Update("update " + DB1TableName.TECH_CAPACITY_RULE + " set is_related_pcba = #{isRelatedPcba} where tech_capacity_rule_id = #{techCapacityRuleId}")
    Integer updateIsRelatedPcba(@Param("isRelatedPcba") Integer isRelatedPcba, @Param("techCapacityRuleId") Integer techCapacityRuleId);
}
